package org.lds.ers.client.widgets;

import java.util.List;

import org.lds.ers.client.LoginService;
import org.lds.ers.client.LoginServiceAsync;
import org.lds.ers.client.model.StakeERSCInfo;
import org.lds.ers.client.model.StakeInfo;
import org.lds.ers.client.model.SysAdminInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MaintainSysAdmin extends VerticalPanel {
	private static MaintainSysAdmin singleton;
	public static MaintainSysAdmin getInstance()
	{
		if (singleton==null)
			singleton = new MaintainSysAdmin();
		
		return singleton;
	}
	
	private LoginServiceAsync loginService = GWT.create(LoginService.class);
	final private VerticalPanel pnlMain ;
	final private VerticalPanel pnlContent;
	final private TextBox txtUserID;
	final private TextBox txtRemark;
	final DecoratorPanel pnlAddNew;
	
	private MaintainSysAdmin()
	{
		pnlMain = new VerticalPanel();
		pnlContent = new VerticalPanel();
		this.pnlMain.add(pnlContent);
		this.add(pnlMain);
		
		refreshSysAdmins();
		
		//Add a panel for insert new ERS records.
		pnlAddNew = new DecoratorPanel();
		pnlAddNew.setVisible(false);
		final Button btnAdd = new Button("儲存");
		btnAdd.setEnabled(false);
		
		FlexTable editForm = new FlexTable();
		editForm.setWidget(0, 0, new Label("使用者ID :"));
		this.txtUserID = new TextBox();
		this.txtUserID.addKeyPressHandler(new KeyPressHandler(){
			public void onKeyPress(KeyPressEvent event) {
				btnAdd.setEnabled(!txtUserID.getText().equals(""));
			}
		});

		editForm.setWidget(0,1,this.txtUserID);
		editForm.setWidget(1, 0, new Label("備註 :"));
		this.txtRemark = new TextBox();
		editForm.setWidget(1,1,this.txtRemark);
		
		btnAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				btnAdd.setEnabled(false);
					SysAdminInfo admin = new SysAdminInfo();
					admin.setUserID(txtUserID.getText());
					admin.setRemark(txtRemark.getText());
					
					loginService.addAdmin(admin, new AsyncCallback<SysAdminInfo>() {

						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							btnAdd.setEnabled(true);
							Window.alert(caught.getMessage());
						}

						public void onSuccess(SysAdminInfo result) {
							// TODO Auto-generated method stub
							txtUserID.setText("");
							txtRemark.setText("");
							btnAdd.setEnabled(false);
							pnlAddNew.setVisible(false);
							refreshSysAdmins();
						}
				    });
				
			}
		});
		editForm.setWidget(1,2, btnAdd);
		
		final Button btnCancel = new Button("取消");
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				pnlAddNew.setVisible(false);
				txtUserID.setText("");
				txtRemark.setText("");
			}
		});
		editForm.setWidget(1,3,btnCancel);
		pnlAddNew.add(editForm);
		
		final Button btnShowAddNew = new Button("新增");
		btnShowAddNew.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event) {
				pnlAddNew.setVisible(true);
			}
		});
		
		this.pnlMain.add(btnShowAddNew);
		
		
		
		this.pnlMain.add(pnlAddNew);

	}
	
	public void refreshSysAdmins()
	{
		loginService.getAdmins(new AsyncCallback<List<SysAdminInfo>>() {

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert(caught.getMessage());
			}

			public void onSuccess(List<SysAdminInfo> result) {
				// TODO Auto-generated method stub
				redrawTable(result);
			}
	    });
	}
	
	private void redrawTable(List<SysAdminInfo> admins) {
		this.pnlContent.clear();
		FlexTable table = new FlexTable();
		table.setBorderWidth(1);
		table.setText(0, 0, "使用者代碼");
		table.setText(0, 1, "備註");
		table.setText(0, 2, "資料更新時間");
		
		int row= 1;
		for(SysAdminInfo a : admins) {
			addRow(row, table, a);
			row = row + 1;
		}
		this.pnlContent.add(table);
	}
	
	private void addRow(int row, FlexTable table, SysAdminInfo a)
	{
		final SysAdminInfo adm = a;
		table.setText(row, 0, a.getUserID());
		table.setText(row, 1, a.getRemark());
		table.setText(row, 2, a.getLastUpdate().toString());
		
		
		final Button btn = new Button("移除");
		btn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (Window.confirm("確定要將『" + adm.getUserID() + "』移出系統管理者名單嗎？"))
				{
					btn.setEnabled(false);
					loginService.removeAdmin(adm.getId(), new AsyncCallback<Boolean>() {
						public void onFailure(Throwable caught) {
							Window.alert(caught.getMessage());
							btn.setEnabled(true);
						}
						
						public void onSuccess(Boolean result) {
							refreshSysAdmins();
							btn.setEnabled(true);
						}
				    });
				}
			}		
		});
		table.setWidget(row, 3, btn);
	}
}
